# -*- coding:utf-8 -*-
"""
    Fundation object
    @Author:ibunny
    @Date: 2021.02.26
"""

from __future__ import division, print_function

class FundationObj(object):
    def __init__(self, fund_code, fund_name, fund_real_valuation, fund_current_valuation, current_rise_rate, current_valuation_time, **kwargs):
        self.fund_code = fund_code
        self.fund_name = fund_name
        self.fund_real_valuation = fund_real_valuation
        self.fund_current_valuation = fund_current_valuation
        self.current_rise_rate = current_rise_rate
        self.current_valuation_time = current_valuation_time
        self.user_fund_hold_price = 0.0
        self.user_fund_hold_num = 0.0
        self.user_current_gain_rate = 0.0
        self.user_estimate_gain_rate = 0.0
        self.user_recmd_cover_money = 0.0
            
    def set_user_fund_hold_price(self, user_fund_hold_price):
        if not isinstance(user_fund_hold_price, float) and not isinstance(user_fund_hold_price, int):
            raise ValueError('user_fund_hold_price must be an number!')
        if user_fund_hold_price < 0 :
            raise ValueError('user_fund_hold_price must > 0')
        self.user_fund_hold_price = user_fund_hold_price

    def set_user_fund_hold_num(self, user_fund_hold_num):
        if not isinstance(user_fund_hold_num, float) and not isinstance(user_fund_hold_num, int):
            raise ValueError('user_fund_hold_num must be an number!')
        if user_fund_hold_num < 0 :
            raise ValueError('user_fund_hold_num must > 0')
        self.user_fund_hold_num = user_fund_hold_num

   def get_fund_name(self):
        return self.fund_name.encode('utf-8')

    def obj2dict(self, cal_user_gain_rate=False):
        if cal_user_gain_rate:
            self.obj_json_format = {'基金代码': self.fund_code, 
                                    '基金全称': self.fund_name, 
                                    '基金净值': self.fund_real_valuation,
                                    '基金当日估值': self.fund_current_valuation, 
                                    '基金当日涨跌率': self.current_rise_rate, 
                                    '基金持仓价格': self.user_fund_hold_price,
                                    '基金持有收益率估值': self.user_estimate_gain_rate,
                                    '建议补仓金额': self.user_recmd_cover_money,
                                    '时间戳': self.current_valuation_time}
        else:
            self.obj_json_format = {'基金代码': self.fund_code, 
                        '基金全称': self.fund_name, 
                        '基金净值': self.fund_real_valuation,
                        '基金当日估值': self.fund_current_valuation, 
                        '基金当日涨跌率': self.current_rise_rate, 
                        '时间戳': self.current_valuation_time}
        return self.obj_json_format


    def cal_user_estimate_gain_rate(self):
        self.user_estimate_gain_rate = round(100*(self.fund_current_valuation / self.user_fund_hold_price - 1), 2)
        

    def cal_user_recmd_cover_money(self, target_return_rate):
        """
            if I wanna keep my rr under target_return_rate, how much should I cover:
        """
        if not isinstance(target_return_rate, float) and not isinstance(target_return_rate, int):
            raise ValueError('target_return_rate must be an number!')
        if self.user_estimate_gain_rate < 0 and self.user_estimate_gain_rate < target_return_rate:
            self.user_recmd_cover_money = ((self.fund_current_valuation - (1+0.01*target_return_rate) * self.user_fund_hold_price) * self.user_fund_hold_num) / (0.01*target_return_rate)
